<?php
// $Id$

/**
 * Controller_Discusstopic 控制器
 */
class Controller_Discusstopic extends Controller_Abstract
{
	function actionPhoneWriteTopic()
	{
        $user_name = $this->_context->user_name;
        $user = User::find('username = ?', $user_name)->query();
        if(!isset($user->username) || $user->username != $user_name || !isset($this->_context->topic_content))
        {
            return 'ERROR';
        }

        $topic_content = $this->_context->topic_content;
        $created_time=time();
        $updated_time=time();
        $enterprise_id=$user->enterprise_id;
        $user_id=$user->user_id;

        $discusstopic = new Discussgroup();
        $discusstopic->topic_content = $topic_content;
        $discusstopic->created_time = $created_time;
        $discusstopic->created_time = $created_time;
        $discusstopic->updated_time = $updated_time;
        $discusstopic->enterprise_id = $enterprise_id;
        $discusstopic->user_id = $user->user_id;
        $discusstopic->save();

        return 'SUCCESS';
	}

	function actionPhoneEditTopic()
	{
        $user_name = $this->_context->user_name;
        $user = User::find('username = ?', $user_name)->query();
        if(!isset($user->username) || $user->username != $user_name)
        {
            return 'ERROR';
        }
        $topic_id = $this->_context->topic_id;
        $discusstopic = Discussgroup::find('group_topic_id  = ?', $topic_id)->query();
        if(!isset($discusstopic->group_topic_id) || $discusstopic->group_topic_id != $topic_id)
        {
            return 'ERROR';
        }
        $discusstopic->topic_content = $topic_content;
        $discusstopic->save();

        return 'SUCCESS';
	}

    function actionPhoneRequieTopicComments()
    {
        $topic_id = $this->_context->topic_id;
        $discusstopic = Discussgroup::find('group_topic_id  = ?', $topic_id)->query();
        if(!isset($discusstopic->group_topic_id) || $discusstopic->group_topic_id != $topic_id)
        {
            return 'ERROR';
        }
        
        $select = Discusscomment::find('topic_id  = ?', $topic_id)->order('comment_id DESC')->all();
        $page = intval($this->_context->page);
        if ($page < 1) $page = 1;

        $select->limitPage($page, Q::ini('appini/web_page_count'));
        
        $comments = $select->getAll();
        
        $col_img="...";
		require 'genrss.php';

        $rss_gen=new RSS("讨论组", $col_img, $select->getCount(), '');
        foreach($comments as $comment)
        { 
            $ran = (rand())%7;
            $user = User::find('user_id  = ?', $comment->user_id)->query();
            $username="...";
            if(isset($user->username))
            {
                $username=$user->username;
            }

            $rss_gen->AddItem($comment->comment_content, 
                "...", $username, Date('Y-m-d H:i:s', $comment->created_time), "...");
        }
        $ret=$rss_gen->Fetch();
        echo $ret;
        return;
    }

	function actionPhoneTopicComment()
	{
        $user_name = $this->_context->user_name;
        $user = User::find('username = ?', $user_name)->query();
        if(!isset($user->username) || $user->username != $user_name)
        {
            return 'failed';
        }
        $topic_id = $this->_context->topic_id;
        $discusstopic = Discussgroup::find('group_topic_id  = ?', $topic_id)->query();
        if(!isset($discusstopic->group_topic_id) || $discusstopic->group_topic_id != $topic_id)
        {
            return 'failed';
        }

        //echo $this->_context->comment_content;

        $comment = new Discusscomment();
        $comment->topic_id = $topic_id;
        $comment->comment_content = $this->_context->comment_content;
        $comment->created_time = time();
        $comment->user_id = $user->user_id;
        $comment->save();

        $discusstopic->comments_count++;
        $discusstopic->save();

        return 'success';
	}

    function actionDisscussRequire()
	{		
        $user_name = $this->_context->user_name;
        $user = User::find('username = ?', $user_name)->query();
        if(!isset($user->username) || $user->username != $user_name)
        {
            return 'failed';
        }
        $topic_id = $this->_context->topic_id;
        $select = Discussgroup::find('enterprise_id  = ?', $user->enterprise_id)->order('group_topic_id DESC')->all();

        $page = intval($this->_context->page);
        if ($page < 1) $page = 1;

        $select->limitPage($page, Q::ini('appini/web_page_count'));
        
        $discusstopics = $select->getAll();
        $date = Date('Y-m-d H:i:s', time()); 
        
        $web_site=Q::ini('appini/web_site_ip');

        $col_img="...";

		require 'genrss.php';

        $rss_gen=new RSS("讨论组", $col_img, $select->getCount(), '');
        foreach($discusstopics as $topic)
        { 
            $user = User::find('user_id  = ?', $topic->user_id)->query();
            $username="...";
            if(isset($user->username))
            {
                $username=$user->username;
            }
            $rss_gen->AddItem($topic->topic_content, $topic->id(), $username, Date('Y-m-d H:i:s', $topic->created_time),  
                $topic->comments_count);
        }
        $ret=$rss_gen->Fetch();
        echo $ret;
        return;
	}

    function actionManageTopic()
    {
		if(null == $this->_app->currentUser())
		{
			return $this->_redirect(url('default/index'));
		}

		$cur_user = $this->_app->currentUserObject();
		if(2 != $cur_user->permission_id)
		{
			return $this->_redirect(url('default/index'));
		}

        $select = Discussgroup::find('enterprise_id  = ?', $cur_user->enterprise_id)->order('group_topic_id DESC')->all();

        $page = intval($this->_context->page);
        if ($page < 1) $page = 1;

        $select->limitPage($page, Q::ini('appini/web_page_count'));
        
		// 将分页信息和查询到的数据传递到视图
		$this->_view['pagination'] = $select->getPagination();
		$this->_view['discusstopics'] = $select->getAll();

		$this->_view['num'] = $select->getCount();
		$this->_viewname = 'index';
    }


	function actionDelete()
	{
		if(null == $this->_app->currentUser())
		{
			return $this->_redirect(url('default/index'));
		}

		$cur_user = $this->_app->currentUserObject();
		if(2 != $cur_user->permission_id)
		{
			return $this->_redirect(url('default/index'));
		}

        if(isset($_POST['checkboxid'])){
			$id=$_POST['checkboxid'];
			
			foreach ($id as $value){
				Discussgroup::meta()->destroyWhere(' group_topic_id = ?', $value);
			}
		}

		return $this->_redirect(url('discusstopic/ManageTopic'));
	}
}


